package com.mo.ld.activity.view.chart

import android.graphics.Color
import android.graphics.Typeface
import android.util.DisplayMetrics
import android.view.WindowManager
import com.github.mikephil.charting.charts.HorizontalBarChart
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mo.ld.databinding.ActBarchartBinding
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.KLog
import com.mo.lib.view.chart.*
import com.xuexiang.xutil.display.ScreenUtils

/**
 * @ author：mo
 * @ data：2022/8/25:17:18
 * @ 功能：
 */
class BarChartActivity2 : BaseVbActivity<ActBarchartBinding>() {
    override fun initData() { //从下往上绘制
        val entries: MutableList<BarEntry> = ArrayList()
        val da = floatArrayOf(11f, 13f, 15f, 17f, 20f, 19f, 16f, 14f, 11f, 6f, 2f)
        val da2 = floatArrayOf(-12f, -13f, -15f, -17f, -19f, -19f, -16f, -13f, -10f, -5f, -1f)
        for (i in da.indices) {
            entries.add(BarEntry(i.toFloat(), floatArrayOf(da[i], da2[i])))
        }
        viewBinding.hbvChart3.setDescription2 {
            isEnabled = true
            text = "全国人口普查"
//            textSize = 15f
//            textColor = Color.BLACK // 获取屏幕中间x 轴的像素坐标
            setPosition((ScreenUtils.getScreenWidth()/3*1.5).toFloat(), 40f)
        }.setLegend2 {
            yOffset = 100f
            xOffset = 5f

        }.setChartSetting2 {
//            (this as HorizontalBarChart).setFitBars(true)
            (this as HorizontalBarChart).setDrawValueAboveBar(true)

        }.setXAxis2 {
            textSize = 8f
//            axisMaximum = 10.6f
//            axisMinimum = -0.6f //把最小值设置为负数能够为下方留出点空白距离
//            setDrawGridLines(false) //不显示网格
//            setDrawAxisLine(true) //将x轴显示在左侧
            position = XAxis.XAxisPosition.BOTH_SIDED
            labelCount = 11 //自定义样式
            valueFormatter = object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return (value.toInt() * 10).toString() + "-" + (value.toInt() + 1) * 10
                }
            } //设置x轴的偏移量
            xOffset = 5f
        }.setAxisLeft2 {
            axisMinimum = -25f
            axisMaximum = 25f
            isEnabled = false
        }.setAxisRight2 {
            textSize = 5f
            axisMinimum = -25f
            axisMaximum = 25f
            textColor = Color.BLACK
            setDrawGridLines(false)
            setDrawLabels(true) //自定义样式
            valueFormatter = object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return Math.abs(value).toString() + " MM"
                }
            }
        }.loadBarData(entries, "", barDataSet = {
            valueTextSize = 5f //柱块上的字体大小
            valueTextColor = Color.BLACK //柱块上的字体颜色
            valueTypeface = Typeface.DEFAULT_BOLD //自自定义样式
            valueFormatter = object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return Math.abs(value.toInt()).toString() + " MM"
                }
            } //设置柱块的颜色
            setColors(Color.GREEN, Color.BLUE)
            stackLabels = arrayOf("女人", "男人")
        }, barData = {
//            barWidth = 0.8f
        }) //    loadData()
    }




    //加载数据
    private fun loadData() { //不绘制图例


        //        val barDataSet = BarDataSet(entries, "")
        //        barDataSet.valueTextSize = 10f //柱块上的字体大小
        //        barDataSet.valueTextColor = Color.BLACK //柱块上的字体颜色
        //        barDataSet.valueTypeface = Typeface.DEFAULT_BOLD //自自定义样式
        //        barDataSet.valueFormatter = object : ValueFormatter() {
        //            override fun getFormattedValue(value: Float): String {
        //                return Math.abs(value.toInt()).toString() + " MM"
        //            }
        //        } //设置柱块的颜色
        //        barDataSet.setColors(Color.GREEN, Color.BLUE)
        //        barDataSet.stackLabels = arrayOf("女人", "男人")
        //        viewBinding.hbvChart3.legend.yOffset = 15f
        //        val barData = BarData(barDataSet) //设置柱块的宽度
        //        barData.barWidth = 0.8f
        //        viewBinding.hbvChart3.data = barData
        //        viewBinding.hbvChart3.invalidate()
    }


}